#!/bin/sh
RTMEUSDIR=`pwd | sed 's/\/tools//'`
RTMTMPDIR=/usr/local/src/OpenRTM
RTCTOOLSTMPDIR=/usr/local/src/rtctools

OPENRTM_VER=OpenRTM-aist-1.0.0
OPENRTM_FILE=$OPENRTM_VER-RELEASE.tar.gz
OPENRTM_SRCURL=http://www.openrtm.org/pub/OpenRTM-aist/cxx/1.0.0/$OPENRTM_FILE

OPENRTM_PYTHON=OpenRTM-aist-Python-1.0.1
OPENRTM_PYTHON_FILE=$OPENRTM_PYTHON.tar.gz
OPENRTM_PYTHON_URL=http://www.openrtm.org/pub/OpenRTM-aist/python/1.0.1/$OPENRTM_PYTHON_FILE

RTCSHELL_VER=rtshell-3.0.0
RTCSHELL_FILE=$RTCSHELL_VER.tar.gz
RTSHELL_SRCURL=http://www.openrtm.org/pub/OpenRTM-aist/tools/1.0.0/$RTCSHELL_FILE

RTCTREE_VER=rtctree-3.0.0
RTCTREE_FILE=$RTCTREE_VER.tar.gz
RTCTREE_SRCURL=http://www.openrtm.org/pub/OpenRTM-aist/tools/1.0.0/$RTCTREE_FILE

RTMEXT_URI=https://github.com/hyaguchijsk/rtmext.git

PYTHON_VER=2.6

if [ "$1" != "--no-rtm-install" ];
then
    if [ ! -e ${RTMTMPDIR} ];
    then
	sudo apt-get remove openrtm-aist openrtm-aist-dev openrtm-aist-doc openrtm-aist-example
	sudo chmod 777 /usr/local/src/
	mkdir -p $RTMTMPDIR
	cd $RTMTMPDIR
	pwd
	wget $OPENRTM_SRCURL
	tar xvzf $OPENRTM_FILE 
	cd $OPENRTM_VER
	./configure --prefix=/usr/local/
	make
	sudo make install
    fi
fi    

if [ ! -e ${RTCTOOLSTMPDIR} ];
then
    sudo apt-get remove openrtm-aist-python openrtm-aist-python-example
    sudo chmod 777 /usr/local/src/
    mkdir -p $RTCTOOLSTMPDIR

    #install openrtm-aist-python
    cd $RTCTOOLSTMPDIR
    wget $OPENRTM_PYTHON_URL
    tar xvzf $OPENRTM_PYTHON_FILE
    cd $OPENRTM_PYTHON
    python setup.py build
    sudo python setup.py install

    sudo rm -rf /usr/local/lib/python$PYTHON_VER/dist-packages/rtshell /usr/local/lib/python$PYTHON_VER/dist-packages/rtshell-3.0.0.egg-info
    cd $RTCTOOLSTMPDIR
    wget $RTSHELL_SRCURL
    tar xvzf $RTCSHELL_FILE
    cd $RTCSHELL_VER
    sudo python setup.py install

    sudo rm -rf /usr/local/lib/python$PYTHON_VER/dist-packages/rtctree /usr/local/lib/python$PYTHON_VER/dist-packages/rtctree-3.0.0.egg-info
    cd $RTCTOOLSTMPDIR
    wget $RTCTREE_SRCURL
    tar xvzf $RTCTREE_FILE
    cd $RTCTREE_VER
    sudo python setup.py install

    cd $RTCTOOLSTMPDIR
    git clone $RTMEXT_URI
fi

#add something in .bashrc.rtm
cd $RTMEUSDIR/tools/
$RTMEUSDIR/tools/setup-bashrc.sh